package vg.modules.navigator.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.skeleton.GraphTreeElement;
import vg.services.data_base_manager.interfaces.IModel;
import vg.services.graph_view_manager.interfaces.graph_comparison.AGraphComparison;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.event_and_request.request.UIRequestAddTab;
import vg.services.user_interface_manager.interfaces.AUserInterface;

/**
 * This class realizes menu for comparison of two graph models.
 * @author tzolotuhin
 */
public class GraphComparisonMenuItem implements IMenuItem {
	private final JMenuItem jItem;
	private final GraphComparisonAction action;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 * @param model - main storage.
	 * @param userInterface - main user interface.
	 */
	public GraphComparisonMenuItem(final JTree tree, final IModel model, final AUserInterface userInterface) {		
		this.action = new GraphComparisonAction(tree, model, userInterface);
		this.jItem = new JMenuItem("Comparison of two graphs");
		//final Icon icon = new ImageIcon("data/resources/textures/workflow.png");
		//this.jItem.setIcon(icon);
		this.jItem.addActionListener(this.action);
	}
	public JMenuItem getMenuItem() {
		return jItem;
	}
	public boolean isVisible() {
		return action.isVisible();
	}
	public void updateTheme() {
		SwingUtilities.updateComponentTreeUI(jItem);
	}
}
class GraphComparisonAction implements ActionListener {
	private final JTree tree;
	private final IModel model;
	private final AUserInterface userInterface;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 * @param model - main storage.
	 */
	public GraphComparisonAction(final JTree tree, final IModel model, final AUserInterface userInterface) {
		this.tree = tree;
		this.model = model;
		this.userInterface = userInterface;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 2) {
				TreeModel tm = this.tree.getModel();
				if(tm == null) {
					MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Openning of comparison of two models(from navigator). Model = null.");
					return;
				} else {
					GraphTreeElement bufNode1 = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
					GraphTreeElement bufNode2 = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[1]);
					
					// get subgraphs
					int sg_id_1 = bufNode1.vg_getGraphId_tmp();
					int sg_id_2 = bufNode2.vg_getGraphId_tmp();
					
					// add request to queue
					if (sg_id_1 >= 0 && sg_id_2 >= 0) {
						Graph sg1 = MainManager.model.getGraph(sg_id_1);
						Graph sg2 = MainManager.model.getGraph(sg_id_2);
						AGraphComparison gc = MainManager.graphViewManager.newGraphComparison(sg1, sg2);
						UIRequestAddTab request = new UIRequestAddTab(gc);  
						userInterface.addRequest(request);
					}
				}
			}
		}	
	}
	public boolean isVisible() {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 2) {
				return true; 
			}
		}
		return false;
	}
}